package com.cn.lp.ai.factory;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * spring容器帮助类
 *
 * @author wuqirong
 * @date 2021-10-27
 * <p>
 * All rights Reserved, Designed www.xiao100.com
 */
@Component
public class SpringAide implements ApplicationContextAware, EnvironmentAware {

    private static ApplicationContext applicationContext;

    private static Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
        SpringAide.environment = environment;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringAide.applicationContext = applicationContext;
    }

    public static void setContext(ApplicationContext context) {
        applicationContext = context;
    }

    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    public static Optional<Environment> getEnvironment() {
        return Optional.ofNullable(environment);
    }
}
